package com.hnevc.androiduiclassone10;

import android.content.Intent;
import android.renderscript.ScriptGroup;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.squareup.picasso.Picasso;

import org.w3c.dom.Text;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.lang.reflect.Type;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    List<Team> teamList;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        initData();
        ListView listView=findViewById(R.id.lv_teams);
        MyTeamAdapter adapter=new MyTeamAdapter();
        listView.setAdapter(adapter);

        Toast.makeText(this,teamList.size()+"",Toast.LENGTH_SHORT).show();
    }

    private void initData() {
        Gson gson=new Gson();
        Type listType=new TypeToken<List<Team>>(){}.getType();
        InputStream in =getResources().openRawResource(R.raw.data);
        Reader reader=new InputStreamReader(in);
        teamList =gson.fromJson(reader,listType);

    }


    class MyTeamAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return teamList.size();
        }

        @Override
        public Object getItem(int position) {
            return teamList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {

            View view =View.inflate(MainActivity.this,R.layout.list_item,null);

            TextView tvZuhao=view.findViewById(R.id.tv_zuhao);
            tvZuhao.setText(teamList.get(position).getZuhao());

            ImageView ivTupian=view.findViewById(R.id.iv_tupian);
            switch (teamList.get(position).getQiudui()){
                case "aodaliya":
                    ivTupian.setImageResource(R.drawable.aidaliya);
                    break;
                case "agenting":
                    ivTupian.setImageResource(R.drawable.agenting);
                    break;
                case "aiji":
                    ivTupian.setImageResource(R.drawable.aiji);
                    break;
                case "banama":
                    ivTupian.setImageResource(R.drawable.banama);
                    break;
                case "baxi":
                    ivTupian.setImageResource(R.drawable.baxi);
                    break;
            }

            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent=new Intent(MainActivity.this,Activity.class);
                    intent.putExtra("team",teamList.get(position));
                    startActivity(intent);
                }
            });

            return view;
        }
    }
}
